//app.js
App({
  onLaunch: function() {
    this.onLoad();
  },
  onLoad: function() {
    //var timestamp = Date.parse(new Date());
    var date = new Date();
    //获取年份  
    var Y = date.getFullYear();
    //获取月份  
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    //获取当日日期 
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    console.log(Y + '-' + M + '-' + D);
    this.globalData.date = Y + '-' + M + '-' + D;
  },
  globalData: {
    userInfo: null,
    date: '',
    url: '',
    imgUrl: '',
    session_id: '',
    studentInfo: {},
    teacherInfo: {},
    open_id: '',
    customReasonList: [],
    hasGetReply: false,
    argument: {},
    bindInfo: {}
  },
  requestPost: function(url, data = {}, success = null, fail = null, error = null, showLoading = false, loading = '', isLogin = false) {
    if(showLoading){
      if(loading == ''){
        wx.showLoading({
          title: '数据加载中',
        })
      }else{
        wx.showLoading({
          title: loading,
        })
      }
      
    }
    if(isLogin){
      var header = {
        'content-type': 'application/x-www-form-urlencoded'
      }
    }else{
      var header = {
        'content-type': 'application/x-www-form-urlencoded',
          'Cookie': 'PHPSESSID=' + this.globalData.session_id
      }
    }
    wx.request({
      url: this.globalData.url + url,
      data: data,
      method: 'POST',
      header: header,
      success: res => {
        if (res.data.success == 1) {
          if(typeof success == 'function'){
            success(res);
          }
        } else if (res.data.success == 0) {
          wx.showModal({
            content: res.data.msg,
            confirmColor: '#1E90FF',
            showCancel: false
          });
          if (typeof fail == 'function') {
            fail(res);
          }
        } else if (res.data.success == -1) {
          wx.showModal({
            content: res.data.msg,
            confirmColor: '#1E90FF',
            showCancel: false,
            success:function(){
              wx.reLaunch({
                url: '/pages/login/login',
              })
            }
          });
          if (typeof fail == 'function') {
            fail(res);
          }
        } else {
          wx.showModal({
            content: '出错了...',
            confirmColor: '#1E90FF',
            showCancel: false
          });
        }
      },
      fail: function() {
        wx.showModal({
          content: '网络出问题了..',
          confirmColor: '#1E90FF',
          showCancel: false
        });
        if (typeof err == 'function') {
          err();
        }
      },
      complete:function(){
        if(showLoading){
          wx.hideLoading();
        }
      }
    })
  }
})